package com.ys.jsontest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //银盛二维码
        //1.1 组装报文-外层报文也就是api文档中的请求参数
        //特别注意 商户在生产环境联调测试，请替换成自己在银盛正式环境申请的商户号（partner_id）

        Map<String, String> mapData = new HashMap<String, String>();
        mapData.put("partner_id", "826584848160007");
        mapData.put("timestamp", DateUtil.getDateNow());
        mapData.put("charset", "utf-8");
        mapData.put("sign_type", "SM");
        mapData.put("notify_url", "http://127.0.0.1");
        mapData.put("version", "3.0");
        mapData.put("method", "ysepay.online.weixin.pay");

        //1.2 组装报文-内层报文也就是api文档中的业务参数
        JSONObject json = new JSONObject();
        try {
            json.put("out_trade_no", "123456789");
            json.put("subject", "公众号");
            json.put("total_amount", "2.99");
            json.put("currency", "CNY");
            json.put("seller_id", "826584848160007");
            json.put("seller_name", "银盛支付服务股份有限公司行业发展部");
            json.put("timeout_express","96h");
            json.put("business_code", "01000010");
            json.put("is_minipg", "1");
            json.put("appid", "wxf71930fbcc125f64");
            json.put("sub_openid","oc0KJ5Kcr5lhbKgmkqXPENrGzsdw");
        } catch (JSONException e) {
            e.printStackTrace();
        }


        mapData.put("biz_content", json.toString());

        List<JSONObject> list1=new ArrayList<JSONObject>();

        //1.3 组装报文-对除sign之外的外层报文参数进行签名,签名之后将sign组装到外层报文
        try {
            Log.e("待签名内容map:{}",mapData.toString());
            //特别注意 商户在生产环境联调测试做签名，请替换成自己操作下载的私钥证书
            //String sign = YsPaySignUtils.sign(this,mapData);
            PrivateKey privateKey = YsPaySignUtils.getPrivateKeyFromAsset(this,mapData);
            //mapData.put("sign", sign);
            mapData.put("biz_content", URLEncoder.encode(mapData.get("biz_content"),"utf-8"));
        } catch (Exception e) {
            Log.e("签名异常，异常信息{}",e.getMessage());
        }
        Log.e("签名之后的报文：{}", CommonUtil.mapToString(mapData));
        //1.4 组装报文完毕，发送请求到银盛网关
        //目前银盛网关没有测试环境可以联调，联调需要访问银盛网关生产环境，生产地址  https://qrcode.ysepay.com/gateway.do
        String result = HttpRequest.sendPost("https://qrcode.ysepay.com/gateway.do", CommonUtil.mapToString(mapData));

    }

    public void compressFile() {
        //压缩到此处
        System.out.println("开始压缩");
        String srcPath = "/mnt/sdcard/TcnLog/log-2022-02-25.txt";
        String path = "/mnt/sdcard/res.zip";
        //要压缩的文件的路径
        File file = new File(path);



        if (file.exists()){
            file.delete();
        }

        try {
            ZipOutputStream zipOutputStream = new ZipOutputStream(new CheckedOutputStream(new FileOutputStream(file),new CRC32()));
            zip(zipOutputStream,"res",new File(srcPath));
            zipOutputStream.flush();
            zipOutputStream.close();
        } catch (Exception e) {
            Log.e("压缩失败",e.getMessage());
        }
        Toast.makeText(this,"压缩完成",Toast.LENGTH_SHORT).show();
    }

    private void zip(ZipOutputStream zipOutputStream,String name, File fileSrc)  throws IOException{

        if (fileSrc.isDirectory()) {
            System.out.println("需要压缩的地址是目录");
            File[] files = fileSrc.listFiles();

            name = name+"/";
            zipOutputStream.putNextEntry(new ZipEntry(name));  // 建一个文件夹
            System.out.println("目录名: "+name);

            for (File f : files) {
                zip(zipOutputStream,name+f.getName(),f);
                System.out.println("目录: "+name+f.getName());
            }

        }else {
            System.out.println("需要压缩的地址是文件");
            zipOutputStream.putNextEntry(new ZipEntry(name));
            System.out.println("文件名: "+name);
            FileInputStream input = new FileInputStream(fileSrc);
            System.out.println("文件路径: "+fileSrc);
            byte[] buf = new byte[1024];
            int len = -1;

            while ((len = input.read(buf)) != -1) {
                zipOutputStream.write(buf, 0, len);
            }

            zipOutputStream.flush();
            input.close();
        }
    }
}